<template>
  <div class="search-box">
    <i class="icon-search"></i>
    <input type="text" ref="query" class="input" v-model="query" :placeholder="placeholder">
    <i class="icon-dismiss" v-show="query.length" @click.stop="clear"></i>
  </div>
</template>

<script>
  import {debunce} from 'common/js/util.js';

  export default {
    props: {
      placeholder: {type: String, default: '搜索歌曲、歌手'}
    },
    data () {
      return {
        query: ''
      };
    },
    methods: {
      clear () {
        this.query = '';
      },
      setQuery (q) {
        this.query = q;
      },
      blurInput () {
        this.$refs.query.blur();
      }
    },
    created () {
      this.$watch('query', debunce((newV) => {
        this.$emit('query', newV);
      }, 700));
    }
  };
</script>

<style lang="less" scoped="">
  @import "~common/css/variable.less";

  .search-box {
    display: flex;
    align-items: center;
    box-sizing: border-box;
    width: 100%;
    padding: 0 6px;
    height: 40px;
    background: @color-highlight-background;
    border-radius: 6px;
    .icon-search {
      font-size: 24px;
      color: @color-background;
    }
    .input {
      flex: 1;
      margin: 0 5px;
      line-height: 18px;
      background: @color-highlight-background;
      color: @color-text;
      font-size: @font-size-medium;
      &::placeholder {
        color: @color-text-d;
      }
    }
    .icon-clear {
      font-size: 16px;
      color: @color-background;
    }
  }
</style>
